<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="分类类别" prop="cid" v-if="formData.cid !== 0">

                </el-form-item>
                <el-form-item label="类别名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入类别名称" :maxlength="100" />
                </el-form-item>

                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越大越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="类别状态">
                    <el-switch v-model="formData.is_show" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="更新时间">
                    <el-date-picker v-model="formData.update_time" type="datetime" placeholder="选择日期时间" :picker-options="{
                        disabledDate(time) {
                            return time.getTime() > Date.now()
                        }
                    }" />
                </el-form-item>
                <el-form-item label="类别内容" prop="content">
                    <el-input type="textarea" v-model="formData.content" placeholder="请输入类别内容" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { computed, ref, reactive, shallowRef } from 'vue'

import { articleEdit, articleAdd, articleDetail, articleAll } from "@/api/news";
// import { deptEdit, deptAdd, deptDetail, deptAll } from "@/api/news.ts";
import Popup from '@/components/popup/index.vue'
// import { useDictOptions } from '@/hooks/useDictOptions'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑类别' : '新增类别'
})
const formData = reactive({
    id: '',
    cid: '' as string | number,
    title: '',
    sort: 0,
    is_show: 1,
    update_time: '',
    content:''


})

const checkMobile = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback()
    } else {
        const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
        console.log(reg.test(value))
        if (reg.test(value)) {
            callback()
        } else {
            return callback(new Error('请输入正确的手机号'))
        }
    }
}
const formRules = {
    cid: [
        {
            required: true,
            message: '请选择上级类别',
            trigger: ['change']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入类别名称',
            trigger: ['blur']
        }
    ]
}

// const apiNeedDetail = ({
//             id: this.form.id
//         }).then(res => {
//             Object.keys(res).map(item => {
//                 this.$set(this.form, item, res[item])
//             })
//         })

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await articleEdit(formData) : await articleAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {

    const { data } = await articleDetail({
        id: row.id
    })
    setFormData(data)

    console.log(data);

}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
